package panels;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;

import util.Func;
import util.util.Hist;
import loot.ChestTable;
import loot.HordeTable;
import loot.PouchTable;

public class LootPanel extends JPanel {

	/**
	 * Loot Panel
	 */
	private static final long serialVersionUID = 1L;
	private JTable foundTable;
	
	private static PouchTable	pouch = new PouchTable();
	private static ChestTable	chest = new ChestTable();
	private static HordeTable	horde = new HordeTable();
	
	public	Hist<String>		found = new Hist<String>();

	public	HashMap<Integer, Hist<String>>	loot = new HashMap<Integer,Hist<String>>();
	
	private JSlider levelSlider;
	
	
	@SuppressWarnings("unused")
	private static final int d(int N, int S) {
		return Func.roll(N, S);
	}
	@SuppressWarnings("unused")
	private static final int d(int S) {
		return Func.roll(S);
	}
	
	
	/**
	 * Create the panel.
	 */
	public LootPanel() {
		int i;
				
		setBorder(null);
		setLayout(new BorderLayout(0, 0));
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBorder(null);
		add(buttonPanel, BorderLayout.NORTH);
		buttonPanel.setLayout(new GridLayout(1, 0, 0, 0));
		
		JButton chestButton = new JButton("Chest");
		chestButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onChestPressed();
			}
		});
		
		JButton pouchButton = new JButton("Pouch");
		pouchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onPouchPressed();
			}
		});
		buttonPanel.add(pouchButton);
		buttonPanel.add(chestButton);
		
		JButton hordeButton = new JButton("Horde");
		hordeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onHordePressed();
			}
		});
		buttonPanel.add(hordeButton);
		
		JButton clearButton = new JButton("Clear");
		clearButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onClearPressed();
			}
		});
		buttonPanel.add(clearButton);
		
		levelSlider = new JSlider();
		levelSlider.setMajorTickSpacing(1);
		levelSlider.setBorder(new TitledBorder(null, "Lvl", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		levelSlider.setValue(1);
		levelSlider.setSnapToTicks(true);
		levelSlider.setPaintLabels(true);
		levelSlider.setPaintTicks(true);
		levelSlider.setOrientation(SwingConstants.VERTICAL);
		levelSlider.setMinorTickSpacing(1);
		levelSlider.setMinimum(1);
		levelSlider.setMaximum(20);
		add(levelSlider, BorderLayout.WEST);
		
		// Add a histogram for each level
		for(i=levelSlider.getMinimum(); i<=levelSlider.getMaximum(); i++) {
			loot.put(new Integer(i), new Hist<String>());
		}
		found = loot.get(new Integer(levelSlider.getValue()));

		// Now add the listener to get past the first error
		levelSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				onSliderChanged();
			}
		});
		
		JPanel resultsPanel = new JPanel();
		resultsPanel.setBorder(new TitledBorder(null, "Loot", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(resultsPanel, BorderLayout.CENTER);
		resultsPanel.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		resultsPanel.add(scrollPane);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		
		foundTable = new JTable();
		foundTable.setBorder(null);
		foundTable.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null},
			},
			new String[] {
				"Treasure", "Quantity"
			}
		));
		foundTable.getColumnModel().getColumn(0).setPreferredWidth(90);
		foundTable.getColumnModel().getColumn(1).setPreferredWidth(30);
		foundTable.addContainerListener(new ContainerAdapter() {
			@Override
			public void componentAdded(ContainerEvent e) {
				e.getComponent().invalidate();
			}
		});
		foundTable.setRowSelectionAllowed(false);
		foundTable.setEnabled(false);
		scrollPane.setViewportView(foundTable);

	}
	
	public void onPouchPressed() {
		Hist<String>	H;
		int i;
		pouch.makeRolls();
		for(i=levelSlider.getMinimum(); i<=levelSlider.getMaximum(); i++) {
			H = loot.get(i);
			H.add(pouch.getLoot(i));
		}
		update();
	}
	public void onChestPressed() {
		Hist<String>	H;
		int i;
		chest.makeRolls();
		for(i=levelSlider.getMinimum(); i<=levelSlider.getMaximum(); i++) {
			H = loot.get(i);
			H.add(chest.getLoot(i));
		}
		update();
	}
	public void onHordePressed() {
		Hist<String>	H;
		int i;
		horde.makeRolls();
		for(i=levelSlider.getMinimum(); i<=levelSlider.getMaximum(); i++) {
			H = loot.get(i);
			H.add(horde.getLoot(i));
		}
		update();
	}
	public void onClearPressed() {
		Hist<String>	H;
		int i;
		for(i=levelSlider.getMinimum(); i<=levelSlider.getMaximum(); i++) {
			H = loot.get(i);
			H.clear();
		}
		update();
	}
	public void onSliderChanged() {
		found = loot.get(levelSlider.getValue());
		System.out.println("levelSlider.value = " + levelSlider.getValue());
		update();
	}
	
	public JSlider getLevelSlider() {
		return levelSlider;
	}
	
	public void update() {
		DefaultTableModel dm = (DefaultTableModel) foundTable.getModel();
		
		String				K;
		Double				V;
		int					row;
		int					numRows = found.keySet().size();
		Iterator<String>	itr = found.keySet().iterator();
		row = 0;
		
		dm.setRowCount(0);
		while(row < numRows && itr.hasNext()) {
			K = itr.next();
			V = found.get(K);
			row++;
			dm.addRow(
				new String[] {
					K, Integer.toString(V.intValue())
				}
			);
		}
	
		dm.fireTableDataChanged();
		foundTable.repaint();
	}
}
